package com.newshub.app.ui.feedback

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import com.newshub.app.R

class FeedbackActivity : AppCompatActivity() {

    private lateinit var toolbar: Toolbar
    private lateinit var feedbackEditText: android.widget.EditText
    private lateinit var contactEditText: android.widget.EditText
    private lateinit var submitButton: android.widget.Button
    private lateinit var progressBar: android.widget.ProgressBar

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        
        try {
            setContentView(R.layout.activity_feedback)
            initViews()
            setupToolbar()
            setupClickListeners()
        } catch (e: Exception) {
            // 如果布局加载失败，显示简单的错误页面
            showErrorPage()
        }
    }

    private fun showErrorPage() {
        val textView = android.widget.TextView(this)
        textView.text = "反馈页面加载失败，请稍后重试"
        textView.textSize = 16f
        textView.gravity = android.view.Gravity.CENTER
        textView.setPadding(32, 32, 32, 32)
        setContentView(textView)
    }

    private fun initViews() {
        try {
            toolbar = findViewById(R.id.toolbar)
            feedbackEditText = findViewById(R.id.feedbackEditText)
            contactEditText = findViewById(R.id.contactEditText)
            submitButton = findViewById(R.id.submitButton)
            progressBar = findViewById(R.id.progressBar)
        } catch (e: Exception) {
            android.util.Log.e("FeedbackActivity", "初始化视图失败", e)
            throw e
        }
    }

    private fun setupToolbar() {
        try {
            setSupportActionBar(toolbar)
            supportActionBar?.setDisplayHomeAsUpEnabled(true)
            supportActionBar?.setDisplayShowHomeEnabled(true)
            supportActionBar?.title = "意见反馈"
            
            toolbar.setNavigationOnClickListener {
                onBackPressed()
            }
        } catch (e: Exception) {
            android.util.Log.e("FeedbackActivity", "设置工具栏失败", e)
        }
    }

    private fun setupClickListeners() {
        try {
            submitButton.setOnClickListener {
                submitFeedback()
            }
        } catch (e: Exception) {
            android.util.Log.e("FeedbackActivity", "设置点击监听器失败", e)
        }
    }

    private fun submitFeedback() {
        try {
            val feedback = feedbackEditText.text.toString().trim()
            val contact = contactEditText.text.toString().trim()

            // 验证输入
            if (TextUtils.isEmpty(feedback)) {
                feedbackEditText.error = "请输入您的意见或建议"
                feedbackEditText.requestFocus()
                return
            }

            if (feedback.length < 10) {
                feedbackEditText.error = "反馈内容至少需要10个字符"
                feedbackEditText.requestFocus()
                return
            }

            // 显示加载状态
            showLoading(true)

            // 模拟提交反馈（实际项目中这里应该调用API）
            android.os.Handler(android.os.Looper.getMainLooper()).postDelayed({
                showLoading(false)
                
                // 显示提交成功提示
                Toast.makeText(this, "反馈提交成功，感谢您的建议！", Toast.LENGTH_LONG).show()
                
                // 清空输入框
                feedbackEditText.text.clear()
                contactEditText.text.clear()
                
                // 可以选择返回上一页或留在当前页面
                // finish()
            }, 2000)
        } catch (e: Exception) {
            android.util.Log.e("FeedbackActivity", "提交反馈失败", e)
            Toast.makeText(this, "提交失败，请重试", Toast.LENGTH_SHORT).show()
        }
    }

    private fun showLoading(show: Boolean) {
        try {
            progressBar.visibility = if (show) View.VISIBLE else View.GONE
            submitButton.isEnabled = !show
            submitButton.text = if (show) "提交中..." else "提交反馈"
        } catch (e: Exception) {
            android.util.Log.e("FeedbackActivity", "显示加载状态失败", e)
        }
    }

    override fun onSupportNavigateUp(): Boolean {
        onBackPressed()
        return true
    }
}
